package com.action.app;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONObject;

import com.operation.GateIP;
import com.util.Log4gUtil;
import com.util.Md5Util;

public class TransferServlet extends HttpServlet {
	/**
	 * Constructor of the object.
	 */
	public TransferServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}
	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("text/html;charset=utf-8");
		request.setCharacterEncoding("UTF-8"); 
		PrintWriter out = response.getWriter();
		JSONObject j = new JSONObject();
		
		String sign = request.getParameter("sign");
		String keyword = request.getParameter("keyword");
		String params =request.getParameter("params");
		
		Log4gUtil.Info("Transfer", params+"", "equest.getParameter", "", "", "");
		String time = request.getParameter("time");
		String token = request.getParameter("token");
		String userid = request.getParameter("userid");
		String NF_callid = request.getParameter("NF_callid");
		
		if(NF_callid!=null&&NF_callid.equals("")){
			NF_callid= Log4gUtil.getNFCallid(NF_callid);
			request.setAttribute("NF_callid",NF_callid);
		}
		//获取关口.代理的公网的地址
		String gateIp = GateIP.getIP(request);
		request.setAttribute("gateIp",gateIp);
		
		if(this.getSign(sign, keyword, token, userid, params, time,NF_callid)){
			if(keyword.equals("version_isAlive"))//验证是否连通
				request.getRequestDispatcher("/ver/version_isAlive.action").forward(request, response);
			
			
			if(keyword.equals("login_resetPwd"))//重设密码登录
				this.verifyToken(j,keyword, token, userid, request,response,"StartPage/login_resetPwd.action");
			if(keyword.equals("version_versionNumber"))//版本更新
				request.getRequestDispatcher("/ver/version_versionNumber.action").forward(request, response);
			if(keyword.equals("url_getlinks"))//框架请求
				request.getRequestDispatcher("/RequestURI/url_getlinks.action").forward(request, response);
			if(keyword.equals("url_pageHome"))//主页请求
				request.getRequestDispatcher("/RequestURI/url_pageHome.action").forward(request, response);
			if(keyword.equals("property_payoffWay"))//模式请求
				request.getRequestDispatcher("/RequestURI/property_payoffWay.action").forward(request, response);
			if(keyword.equals("url_cameraUrl"))//摄像头
				request.getRequestDispatcher("/RequestURI/url_cameraUrl.action").forward(request, response);
			if(keyword.equals("ticket_userTicket"))//查询可领取券
				this.verifyToken(j,keyword, token, userid, request,response,"/matter/ticket_userTicket.action");
			if(keyword.equals("ticket_getTicket")) // 领取券	
				this.verifyToken(j,keyword, token, userid, request,response,"/matter/ticket_getTicket.action");
			if(keyword.equals("ticket_ticketList"))//券包列表
				this.verifyToken(j,keyword, token, userid, request,response,"/matter/ticket_ticketList.action");
			
			if(keyword.equals("mycenter_headImgUrl"))//用户头像
				this.verifyToken(j,keyword, token, userid, request,response,"/userCenter/mycenter_headImgUrl.action");
			if(keyword.equals("mycenter_userImgUrl"))//用户图片
				this.verifyToken(j,keyword, token, userid, request,response,"/userCenter/mycenter_userImgUrl.action");
			if(keyword.equals("mycenter_deleteUserImg"))//删除用户图片
				this.verifyToken(j,keyword, token, userid, request,response,"/userCenter/mycenter_deleteUserImg.action");
			if(keyword.equals("binding_addBinding"))//添加绑定
				this.verifyToken(j,keyword, token, userid, request,response,"/userCenter/binding_addBinding.action");
			if(keyword.equals("binding_relieveBinding"))//解除绑定
				this.verifyToken(j,keyword, token, userid, request,response,"/userCenter/binding_relieveBinding.action");
			if(keyword.equals("binding_bindingList"))//绑定列表
				this.verifyToken(j,keyword, token, userid, request,response,"/userCenter/binding_bindingList.action");;
			if(keyword.equals("binding_areaList"))//区域列表
				this.verifyToken(j,keyword, token, userid, request,response,"/userCenter/binding_areaList.action");
			
		
			if(keyword.equals("authcode_PhoneVerification"))//手机验证码
				request.getRequestDispatcher("/StartPage/authcode_PhoneVerification.action").forward(request, response);
			if(keyword.equals("login_visitorLogin"))//用户token登录及游客登录
				request.getRequestDispatcher("/StartPage/login_visitorLogin.action").forward(request, response);
			if(keyword.equals("login_accountEnroll"))//app账号登录或注册
				request.getRequestDispatcher("/StartPage/login_accountEnroll.action").forward(request, response);
			//上网
			if(keyword.equals("network_verifyNet"))//网络上线(验证)
				request.getRequestDispatcher("/userCenter/network_verifyNet.action").forward(request, response);
			if(keyword.equals("network_planNet"))//预约上网
				this.verifyToken(j,keyword, token, userid, request,response,"/userCenter/network_planNet.action");
			if(keyword.equals("network_planInNet"))//预约时间内上网
				this.verifyToken(j,keyword, token, userid, request,response,"/userCenter/network_planInNet.action");
			if(keyword.equals("network_planAdd"))//会员预约时间内 连接网络
				this.verifyToken(j,keyword, token, userid, request,response,"/userCenter/network_planAdd.action");
			if(keyword.equals("network_linkedNet"))//网络上线(直接连接)
				this.verifyToken(j,keyword, token, userid, request,response,"/userCenter/network_linkedNet.action");
			if(keyword.equals("network_timingNet"))//免费时长上网
				this.verifyToken(j,keyword, token, userid, request,response,"/userCenter/network_timingNet.action");
			//钱包
			if(keyword.equals("deal_selectBalance"))//余额查询
				this.verifyToken(j,keyword, token, userid, request,response,"/matter/deal_selectBalance.action");
			if(keyword.equals("deal_saveBalance"))//余额充值
				request.getRequestDispatcher("/matter/deal_saveBalance.action").forward(request, response);
			if(keyword.equals("deal_feeBalance"))//消费余额
				request.getRequestDispatcher("/matter/deal_feeBalance.action").forward(request, response);
			if(keyword.equals("deal_saveVipmember"))//新增VIP
				request.getRequestDispatcher("/matter/deal_saveVipmember.action").forward(request, response);
			if(keyword.equals("deal_delVipmember"))//删除VIP
				request.getRequestDispatcher("/matter/deal_delVipmember.action").forward(request, response);
			if(keyword.equals("deal_selectVipmember"))//查询VIP
				this.verifyToken(j,keyword, token, userid, request,response,"/matter/deal_selectVipmember.action");
			if(keyword.equals("deal_selectTransaction"))//查询交易明细
				this.verifyToken(j,keyword, token, userid, request,response,"/matter/deal_selectTransaction.action");
			if(keyword.equals("deal_saveCombo"))//新增套餐
				request.getRequestDispatcher("/matter/deal_saveCombo.action").forward(request, response);
			
			//二维码
			if(keyword.equals("accredit_verifyNetQr"))//验证授权二维码 
				this.verifyToken(j,keyword, token, userid, request,response,"/qrcode/accredit_verifyNetQr.action");
			if(keyword.equals("accredit_createNetQr"))//生成二维码内容 
				this.verifyToken(j,keyword, token, userid, request,response,"/qrcode/accredit_createNetQr.action");
			if(keyword.equals("accredit_checkNetQr"))//长轮循
				request.getRequestDispatcher("/qrcode/accredit_checkNetQr.action").forward(request, response);
			if(keyword.equals("pcNetQr_createNetQr"))//pc生成二维码内容 
				request.getRequestDispatcher("/qrcode/pcNetQr_createNetQr.action").forward(request, response);
			if(keyword.equals("pcNetQr_pcQrselfNet"))//pc本人联网操作
				this.verifyToken(j,keyword, token, userid, request,response,"/qrcode/pcNetQr_pcQrselfNet.action");
			if(keyword.equals("pcNetQr_pcQrotherNet"))//pc别人联网标记
				this.verifyToken(j,keyword, token, userid, request,response,"/qrcode/pcNetQr_pcQrotherNet.action");
			if(keyword.equals("pcNetQr_otherNet"))//pc别人联网操作
				request.getRequestDispatcher("/qrcode/pcNetQr_otherNet.action").forward(request, response);
			if(keyword.equals("pcNetQr_checkNetQr"))//pc二维码扫描
				request.getRequestDispatcher("/qrcode/pcNetQr_checkNetQr.action").forward(request, response);
			
			if(keyword.equals("login_accountEnrollPC"))//pc登录
				request.getRequestDispatcher("/StartPage/login_accountEnrollPC.action").forward(request, response);
			
			if(keyword.equals("accredit_infoNetQr"))//二维码扫描检查登录
				this.verifyToken(j,keyword, token, userid, request,response,"/qrcode/accredit_infoNetQr.action");
			if(keyword.equals("QRManage_userQRVerify"))//二维码授权登录
				this.verifyToken(j,keyword, token, userid, request,response,"/qrcode/QRManage_userQRVerify.action");
		
			if(keyword.equals("network_offNet"))//网络下线
				request.getRequestDispatcher("/userCenter/network_offNet.action").forward(request, response);
			if(keyword.equals("gate_getAreaIp"))//验证是否为牛郎网络并给与代理服务器地址
				request.getRequestDispatcher("/gate_getAreaIp.action").forward(request, response);
			if(keyword.equals("network_noFeelNet"))//无感知连网
				request.getRequestDispatcher("/NoVerify/network_noFeelNet.action").forward(request, response);
			
			if(keyword.equals("network_weixinEnrollNet")) //微信网
				request.getRequestDispatcher("/NoVerify/network_weixinEnrollNet.action").forward(request, response);
			
			if(keyword.equals("network_roundNet"))//轮循连网
				request.getRequestDispatcher("/NoVerify/network_roundNet.action").forward(request, response);
			if(keyword.equals("network_roundNet"))//轮循连网
				request.getRequestDispatcher("/NoVerify/network_roundNet.action").forward(request, response);
			
			if(keyword.equals("network_tempNet"))//临时连网
				request.getRequestDispatcher("/NoVerify/network_tempNet.action").forward(request, response);
			if(keyword.equals("PC_userVerfiy"))//验证
				request.getRequestDispatcher("/NoVerify/PC_userVerfiy.action").forward(request, response);
			if(keyword.equals("PC_setBingdingInfo"))//设置更新绑定信息
				request.getRequestDispatcher("/NoVerify/PC_setBingdingInfo.action").forward(request, response);
			if(keyword.equals("PC_delBingding"))//删除绑定信息
				request.getRequestDispatcher("/NoVerify/PC_delBingding.action").forward(request, response);
			if(keyword.equals("PC_verifyBinding"))//验证绑定信息
				request.getRequestDispatcher("/NoVerify/PC_verifyBinding.action").forward(request, response);
			//地图
			if(keyword.equals("map_showMap"))//显示地图
				request.getRequestDispatcher("/matter/map_showMap.action").forward(request, response);
			if(keyword.equals("map_outNetInfo"))//断网地图信息
				request.getRequestDispatcher("/matter/map_outNetInfo.action").forward(request, response);
			if(keyword.equals("map_netInfoList"))//上网记录集合 
				this.verifyToken(j,keyword, token, userid, request,response,"/matter/map_netInfoList.action");
			
			if(keyword.equals("recomment_outputCode"))//用户生成推荐码
				this.verifyToken(j,keyword, token, userid, request,response,"/matter/recomment_outputCode.action");
			if(keyword.equals("mycenter_updataUser"))//修改昵称
				this.verifyToken(j,keyword, token, userid, request,response,"/userCenter/mycenter_updataUser.action");
			if(keyword.equals("mycenter_saveFeedback"))//反馈接口
				this.verifyToken(j,keyword, token, userid, request,response,"/userCenter/mycenter_saveFeedback.action");
			if(keyword.equals("userContent_updateContent"))//修改用户内容
				this.verifyToken(j,keyword, token, userid, request,response,"/userCenter/userContent_updateContent.action");
			if(keyword.equals("userContent_updatePhone"))//修改用户手机号码
				this.verifyToken(j,keyword, token, userid, request,response,"/userCenter/userContent_updatePhone.action");
			if(keyword.equals("loginout_loginOut"))//退出登录
				this.verifyToken(j,keyword, token, userid, request,response,"/userCenter/loginout_loginOut.action");
			
			if(keyword.equals("version_testDate"))//提交测试数据
				request.getRequestDispatcher("/ver/version_testDate.action").forward(request, response);
		}else{
			j.put("code", -1);
			j.put("msg", "签名不对");
		}
		out.print(j);
	}

	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
			this.doGet(request, response);
	}
	
	/**
	 * 验证token操作
	 * 
	 */
	private void verifyToken(JSONObject j,String keyword,String token, String userid ,HttpServletRequest request,HttpServletResponse response,String url) throws ServletException, IOException{
		 if(token!=null&&!token.equals("")){
			 ServletContext application = request.getSession().getServletContext();
			 if(application.getAttribute(userid)!=null){
				 String test = application.getAttribute(userid).toString();
					if(test.equals(token)){
						request.getRequestDispatcher(url).forward(request, response);
					}else{
						j.put("code",-2);
					}
				}else{
					j.put("code",-2);
				}
			}else{
				j.put("code",-2);
			}
	}
	
	/**
	 * 验证签名
	 * @param sign
	 * @param keyword
	 * @param token
	 * @param userid
	 * @param params
	 * @param time
	 * @return
	 */
	private boolean getSign(String sign,String keyword,String token,String userid,String params,String time,String NF_callid){
		String si ="keyword="+keyword+"&params="+params+"&time="+time+"&token="+token+"&userid="+userid+"&key=niulang2016";
		Log4gUtil.Info("TransferServlet", userid+"", "签名内容", si, NF_callid, token);
		boolean flg = false;
		String md5 = Md5Util.getMd5(si);
		if(sign!=null&&!sign.equals("")&&sign.equals(md5)&&System.currentTimeMillis()-Long.parseLong(time)<300000){
			flg=true;
		}else{
			Log4gUtil.Info("TransferServlet", userid+"", "sign不合", "ser:"+sign+"--app:"+md5, NF_callid, token);
		}
		return flg;
	}
	

	public void init() throws ServletException {
		// Put your code here
	}

}
